package com.bbzn.device.client.service.emq.data;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.dataobject.Device;
import com.bbzn.device.client.dto.device.DeviceDTO;
import com.bbzn.device.client.service.DeviceService;
import com.bbzn.device.client.service.EmqStrategy;
import com.bbzn.device.client.utils.PageInfo;
import com.bbzn.device.client.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: jcnehuang
 * @Date : 2021/4/9 11:05
 * @Version 1.0
 * 统计设备使用量
 */
@Slf4j
@Component
public class EmqDeviceUsedStrategy implements EmqStrategy {

    @Autowired
    DeviceService deviceService;
    @Autowired
    RedisUtils redisUtils;

    @Override
    public String emqAction(String payLoad) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(payLoad);
            String imei = jsonObject.getString("imei");
            if (StringUtils.isEmpty(imei)) {
                return "imei错误！卡号不能为空！";
            }
            DeviceDTO device = new DeviceDTO();
            device.setImei(imei);
            device.setPageNum(1);
            device.setPageSize(10);
            device.setStartIndex((device.getPageNum() - 1) * device.getPageSize());
            PageInfo<Device> devices = deviceService.findPageByAll(device);

            if (null == devices || CollectionUtils.isEmpty(devices.getResultList()) || devices.getResultList().stream().anyMatch(d -> null == d.getUserId() || null == d.getProductId())) {
                return "统计设备使用量失败！没有找到对应的设备数据";
            }
            Long userId = devices.getResultList().get(0).getUserId();
            Long productId = devices.getResultList().get(0).getProductId();
            String dateStr = DateUtils.formatDate(new Date(), "yyyy-MM-dd");

            //redis改造  一级key  key:DeviceUsedCount  hkey:tid value:tid:时间  二级key  key:tid:时间  hkey:pid  value:数量
            if (redisUtils.hExists("DeviceUsedCount:" + userId + ":" + dateStr, productId.toString())) {
                redisUtils.hIncrBy("DeviceUsedCount:" + userId + ":" + dateStr, productId.toString(), 1);
                redisUtils.hIncrBy("DeviceUsedCount:" + 0 + ":" + dateStr, productId.toString(), 1);
            } else {
                redisUtils.hPut("DeviceUsedCount:" + userId + ":" + dateStr, productId.toString(), "1");
                redisUtils.hPut("DeviceUsedCount:" + 0 + ":" + dateStr, productId.toString(), "1");
            }


            //智能安全头盔
            if (devices.getResultList().get(0).getProductId() == 1015) {
                String sku = devices.getResultList().get(0).getSku();
                if (StringUtils.isEmpty(sku)) {
                    return null;
                }
                JSONObject object = JSONObject.parseObject(sku);
                if (!object.containsKey("color")) {
                    return null;
                }
                String color = object.getString("color");
                if (redisUtils.hExists("HelmetDevUsedCount:" + userId + ":" + dateStr, color)) {
                    redisUtils.hIncrBy("HelmetDevUsedCount:" + userId + ":" + dateStr, color, 1);
                    redisUtils.hIncrBy("HelmetDevUsedCount:" + 0 + ":" + dateStr, color, 1);
                } else {
                    redisUtils.hPut("HelmetDevUsedCount:" + userId + ":" + dateStr, color, "1");
                    redisUtils.hPut("HelmetDevUsedCount:" + 0 + ":" + dateStr, color, "1");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return "统计设备使用量失败！";
        }
        return "统计设备使用量成功！";
    }
}
